package service;

import dao.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.po.Role;
import pojo.po.User;
import pojo.vo.PageBean;
import pojo.vo.Pagination;
import pojo.vo.Result;
import utils.SqlSessionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserServiceImpl implements UserService{
    public PageBean  FindAllUser(Pagination page){
        PageBean pageBean = new PageBean();
        SqlSession session = SqlSessionUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //获取条件对象
        Map queryParams = page.getQueryParams();
        String username = (String)queryParams.get("username");
        User user = new User();
        user.setUsername(username);
        Integer index = (page.getCurrentPage()-1)*page.getPageSize();
        List<User> users =  mapper.findAllUsers(user,index,page.getPageSize());
        int total = mapper.countUsers(user);
        pageBean.setTotal(total);
        pageBean.setRows(users);
        return pageBean;
    }

    public int deleteUserById(String id) {
        SqlSession session = SqlSessionUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int r1 = mapper.deleteUserFromUR(id);
        int r2= mapper.deleteUserById(id);
        session.close();
        if(r2 ==1&& r1!=0){
            return 1;
        }else if(r2 ==1 && r1==0){
            return 0;
        }else {
            return -1;
        }
    }

    public int updateUser(User user) {
        SqlSession session = SqlSessionUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int r = mapper.updateUser(user);
        Set<Role> roles = user.getRoles();
        ArrayList<String> rids = new ArrayList<>();
        for (Role rid :roles){
            rids.add(rid.getId());
        }
        System.out.println("rids:"+rids);
        mapper.deleteUserFromUR(user.getId());
        mapper.addUserFromUR(user.getId(),rids);
        return r;
    }

    public int addUser(User user) {
        SqlSession session = SqlSessionUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int r = mapper.addUser(user);
        Set<Role> roles = user.getRoles();
        ArrayList<String> rids = new ArrayList<>();
        for (Role rid :roles){
            rids.add(rid.getId());
        }
        System.out.println("rids:"+rids);
        System.out.println("主键回填后："+user);
        mapper.addUserFromUR(user.getId(),rids);
        return r;
    }

//    public Result findUserByLike(Pagination page) {
//        SqlSession session = SqlSessionUtil.getSession();
//        UserMapper mapper = session.getMapper(UserMapper.class);
//        Map queryParams = page.getQueryParams();
//        String username = (String)queryParams.get("username");
//        User user = new User();
//        user.setUsername(username);
//        Integer index = (page.getCurrentPage()-1)*page.getPageSize();
//        List<User> users =  mapper.findUserByLike(username,index,page.getPageSize());        System.out.println("users:"+users);
//        System.out.println("数量："+users.size());
//        PageBean pageBean = new PageBean();
//        int total = mapper.countUsersByLike(username);
//        pageBean.setTotal(total);
//        pageBean.setRows(users);
//        Result result = new Result();
//        System.out.println("total:"+total);
//        if(users!=null){
//            result.setFlag(true);
//            result.setMsg("共查询到"+total+"条结果");
//            result.setData(pageBean);
//        }else {
//            result.setFlag(false);
//            result.setMsg("结果为空");
//        }
//
//        return result;
//    }
}
